<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.1.1/jquery.min.js"></script>
    <script src="bootstrap/js/bootstrap.js"></script>
    <link rel="stylesheet" href="bootstrap/css/bootstrap.css">
    <style>
        .block-content {
            display: flex;
            flex-wrap: wrap;
            /*background-color: #ffff00;*/
            margin-left: 50px;

        }

        .goods-list {
            width: 25%;
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
        }

        .goods-block {
            width: 260px;
            padding: 0 30px;
        }

        .goods-img {
            width: 200px;
            height: 200px;
        }

        .foot {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .add-card {
            width: 30px;
            height: 30px;
        }

        .mt20 {
            margin-top: 20px;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">雷欧shop</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
<!--                <li class="active"><a href="#">Link <span class="sr-only">(current)</span></a></li>-->
                <li><a href="#">购物车</a></li>
<!--                <li class="dropdown">-->
<!--                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">Dropdown <span class="caret"></span></a>-->
<!--                    <ul class="dropdown-menu">-->
<!--                        <li><a href="#">Action</a></li>-->
<!--                        <li><a href="#">Another action</a></li>-->
<!--                        <li><a href="#">Something else here</a></li>-->
<!--                        <li role="separator" class="divider"></li>-->
<!--                        <li><a href="#">Separated link</a></li>-->
<!--                        <li role="separator" class="divider"></li>-->
<!--                        <li><a href="#">One more separated link</a></li>-->
<!--                    </ul>-->
<!--                </li>-->
            </ul>
            <form class="navbar-form navbar-left">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="Search">
                </div>
                <button type="submit" class="btn btn-default">查询</button>
            </form>
            <ul class="nav navbar-nav navbar-right">
                <li><a href="" style="color: red"><span id="loginUser"></span></a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">系统功能 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#" data-toggle="modal" data-target="#myModal">注册</a></li>
                        <li><a href="#" data-toggle="modal" data-target="#loginModal">登录</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#" onclick="exitSys()">安全退出</a></li>
                    </ul>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<div class="block-content" id="blockData">
<!--    <div class="goods-list">-->
<!--        <div class="goods-block">-->
<!--            <img src="http://bigdata2cv2.oss-cn-beijing.aliyuncs.com/C%3A%5Cfakepath%5C%E5%9B%BE%E7%89%871.jpg"-->
<!--                 alt="" class="goods-img">-->
<!--            <div class="foot mt20">-->
<!--                <div class="goods-name">名称</div>-->
<!--                <img src="./img/cart.png"-->
<!--                     alt="" class="add-card">-->
<!--            </div>-->
<!--            <div class="foot mt20">-->
<!--                <div class="new-price">30.60元</div>-->
<!--                <div class="old-price">50.00元</div>-->
<!--            </div>-->
<!--        </div>-->
<!--    </div>-->
</div>









<!--注册模态框-->
    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="myModalLabel">Modal title</h4>
                </div>
                <div class="modal-body">
                    <form class="form-horizontal">
                        <div class="form-group">
                            <label for="inputAccount" class="col-sm-2 control-label">账号</label>
                            <div class="col-sm-10">
                                <input type="email" class="form-control" id="inputAccount" placeholder="请输入账号">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputPassword" class="col-sm-2 control-label">密码</label>
                            <div class="col-sm-10">
                                <input type="password" class="form-control" id="inputPassword" placeholder="请输入密码">
                            </div>
                        </div>
                        <div class="form-group">
                            <label for="inputPassword" class="col-sm-2 control-label">确认密码</label>
                            <div class="col-sm-10">
                                <input type="password" class="form-control" id="inputRePassword" placeholder="请确认密码">
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="creg()">注册</button>
                </div>
            </div>
        </div>
    </div>

<!--登录-->
<!-- Modal -->
<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myLoginModalLabel">Modal title</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label for="inputAccount" class="col-sm-2 control-label">账号</label>
                        <div class="col-sm-10">
                            <input type="email" class="form-control" id="inputLoginAccount" placeholder="请输入账号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword" class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="inputLoginPassword" placeholder="请输入密码">
                            <span id ="tip" style="color : red"></span>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
                <button type="button" class="btn btn-primary" onclick="clogin()">登录</button>
            </div>
        </div>
    </div>
</div>
</div>
</body>
</html>
<script>

    //登录
    function clogin(){
        //获取账号和密码
      let  account = document.getElementById("inputLoginAccount").value;
      let  password = document.getElementById("inputLoginPassword").value;
      //判断账号和密码是否为空
      if (account === "" || password === ""){
          document.getElementById("tip").innerText = "账号或密码不能为空！";
          return;
      }
      document.getElementById("tip").innerText = "";
      //发送请求
      $.ajax({
              url: "clogin",
              type: "POST",
              data: {
                      account: account,
                      password: password
              },
              dataType: "text",
              success: function (res) {
                  if (res === "Y"){
                      document.getElementById("inputLoginPassword").value='';
                      document.getElementById("inputLoginPassword").value='';

                      $("#loginModal").modal("hide");
                      localStorage.setItem('loginUser',account)
                      document.getElementById("loginUser").innerText = account;
                  }else{
                      document.getElementById("tip").innerText = "账号或密码错误！";
                  }
              }
      });
    }



    window.onload = function () {
        loadGoods();
        //检查当前用户是否登录
        checkLogin();
    }
    function checkLogin(){
       let loginUser =  localStorage.getItem('loginUser')

        if (loginUser !== null){
            //显示欢迎xxxx登录
            document.getElementById("loginUser").innerText = loginUser;

        }
    }
    //获取商品信息（可销售的商品）
    function loadGoods() {
        $.ajax({
            url: "loadGoods",
            type: "GET",
            dataType: "text",
            success: function (res) {
                // console.log("消费者页面", res);
              let goods_obj = JSON.parse(res);
              if (goods_obj.code === 200){
                  let goods_list = goods_obj.data;
                  for (let i = 0; i < goods_list.length; i++) {
                      $("<div class='block-content'>\n" +
                          "    <div class='goods-list'>\n" +
                          "        <div class='goods-block'>\n" +
                          "            <img src='"+goods_list[i].img+"'\n" +
                          "                 alt='' class='goods-img'>\n" +
                          "            <div class='foot mt20'>\n" +
                          "                <div class='goods-name'>"+goods_list[i].title+"</div>\n" +
                          "                <img src='./img/cart.png'\n" +
                          "                     alt='' class='add-card'>\n" +
                          "            </div>\n" +
                          "            <div class='foot mt20'>\n" +
                          "                <div class='new-price'>"+(goods_list[i].price*0.8)+"元</div>\n" +
                          "                <div class='old-price'>"+goods_list[i].price+"元</div>\n" +
                          "            </div>\n" +
                          "        </div>\n" +
                          "    </div>\n" +
                          "</div>").appendTo("#blockData");
                  }
              }
        }
        });
    }

//安全退出
    function exitSys() {
        let ans = window.confirm("确认退出?");
        if (ans) {
            $.ajax({
                url: "exitSys",
                type: "post",
                dataType: "text",
                success: function (res) {
                    if (res === "Y") {
                        localStorage.removeItem('loginUser');
                        document.getElementById("loginUser").innerText = "";
                        window.location.reload();//退出后刷新页面
                    }
                }
            });
        }
    }

    //注册
    function creg(){
        //获取账号和密码
        let account = document.getElementById("inputAccount").value;
        let password = document.getElementById("inputPassword").value;
        let repassword = document.getElementById("inputRePassword").value;
        //判断密码是否一致
        if(password !== repassword){
            alert("两次密码不一致！");
            return;
        }
        //发送请求
        $.ajax({
            url: "creg",
            type: "POST",
            data: {
                account: account,
                password: password
            },
            dataType: "text",
            success: function(res) {
                if(res === "Y"){
                    alert("注册成功！");
                    document.getElementById("inputAccount").value='';
                    document.getElementById("inputPassword").value='';
                    document.getElementById("inputRePassword").value='';
                    $("#myModal").modal("hide");
                }else{
                    alert("注册失败！");
                }
            }
        });
    }
</script>